﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using iLangl.LanguageWire.DAL.LanguageWireDAL.RealStorage;
using iLangl.LanguageWire.DAL.iLanglDAL;

namespace iLangl.LanguageWire.Managers
{
	public class ContentManager : BaseManager
	{
		#region Singelton Pattern
		private ContentManager()
		{
		}

		private static ContentManager instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the ContentManager.
		/// </summary>
		public static ContentManager Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new ContentManager();
						}
					}
				}
				return instance;
			}
		}
		#endregion

		public void DownloadContent(Translation translation, TranslationStatuses.eTranslationStatus status, TranslationStatuses.eTranslationStatus lwstatus)
		{

			//Download Target Content
			ContentEntity[] contents = DALFacadeLWAPI.ProjectDAL.DownloadSourceContent(translation);
			List<ContentEntity> validcontent = new List<ContentEntity>();

			if (contents.Length == 1)
			{
				translation.TargetContent = contents[0];
				DALFacade.Instance.ProjectDAL.CreateContent(contents[0], null);

				if (ContentManager.Instance.ValidateContent(translation))
				{
					validcontent.Add(translation.TargetContent);
				}
			}
			else
			{
				//Add Content
				foreach (ContentEntity ce in contents)
				{
					DALFacade.Instance.ProjectDAL.CreateContent(ce, null);
				}

				//Validate content
				foreach (ContentEntity ce in contents)
				{
					translation.TargetContent = ce;

					if (ContentManager.Instance.ValidateContent(translation))
					{
						validcontent.Add(ce);
					}
				}
			}

			if ((contents.Length == 0) || (validcontent.Count == 0))
			{
				//TODO: Generate Alert
			}

			if ((contents.Length == 1) && (validcontent.Count == 1))
			{
				translation.TargetContent = validcontent[0];
			}
			else
			{
				//If more then one valid content human resolution required
				
				bool isthesame = true;
				for(int i = 0; i < validcontent.Count - 1; i++)
				{
					ContentEntity source1 = validcontent[i];
					ContentEntity source2 = validcontent[i+1];

					isthesame = ValidateData(source1, source2);

					if (!isthesame)
						break;
				}
				//If the same doesn't matter
				if (isthesame)
				{
					//Just take the first one
					translation.TargetContent = validcontent[0];
				}
				else
				{
					//TODO: Generate Alert. More then one translated content. What is actual?
				}
			}
		}

		public bool ValidateContent(Translation translation)
		{
			return ValidateStructure(translation.SourceContent, translation.TargetContent);
		}

		public bool ValidateStructure(ContentEntity source, ContentEntity target)
		{
			XMLManager.XMLValidationResult validationresult  = XMLManager.Instance.Compare(source.Content, target.Content);

			return validationresult.IsStructureTheSame;
		}

		public bool ValidateData(ContentEntity source, ContentEntity target)
		{
			XMLManager.XMLValidationResult validationresult = XMLManager.Instance.Compare(source.Content, target.Content);

			return validationresult.IsDataTheSame;
		}

		
	}
}
